package com.xiaozheng.service.impl;

import com.xiaozheng.dao.AccountDao;
import com.xiaozheng.domain.Account;
import com.xiaozheng.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Package: com.xiaozheng.service.impl
 * @ClassName: AccountServiceImpl
 * @Author: 小政同学    QQ:xiaozheng666888@qq.com
 * @CreateTime: 2020/9/25 12:43
 * @What_is_this_file_for: 这个文件是用来做什么的?
 * @Description: 描述
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao dao;

    /*public void setDao(AccountDao dao) {
        this.dao = dao;
    }*/

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<Account> findAllAcount() {
        return dao.findAllAcount();
    }

    /**
     * 根据id查询一个
     *
     * @return
     */
    @Override
    public Account findAccountById(Integer AccountId) {
        return dao.findAccountById(AccountId);
    }

    /**
     * 保存
     *
     * @param account
     */
    @Override
    public void saveAccount(Account account) {
            dao.saveAccount(account);
    }

    /**
     * 更新
     *
     * @param account
     */
    @Override
    public void updateAccount(Account account) {
        dao.updateAccount(account);
    }

    /**
     * 根据id删除
     *
     * @param AccountId
     */
    @Override
    public void deleteAccount(Integer AccountId) {
        dao.deleteAccount(AccountId);
    }

    /**
     * 补充共功能
     * 转账
     *
     * @param sourceUid 转出账户名称
     * @param targetUid 转入账户名称
     * @param money     转账金额
     */
    @Override
    public void transferAccount(String sourceUid, String targetUid, Float money) {

            //1.根据Uid查询转出账户
            Account source = dao.findAccountByUid(sourceUid);
            //2.根据Uid查询转入账户
            Account target = dao.findAccountByUid(targetUid);
            //3.转出账户减钱
            source.setMoney(source.getMoney() - money);
            //int i = 1/0;
            //4.转入账户加钱
            target.setMoney(target.getMoney() + money);
            //5.更新转出账户
            dao.updateAccount(source);
            //6.跟新转入账户
            dao.updateAccount(target);
    }
}
